/*
 * 文件名：MiaoDiSmsSender.java
 * 版权：Copyright by www.taohuakeji.com
 * 描述：
 * 修改人：ying
 * 修改时间：2018-1-3
 */

package com.videoweb.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;

import com.alibaba.fastjson.JSONObject;

/**
 * 秒滴短信发送
 * @author ying
 * @version 2018-1-3
 * @see MiaoDiSmsSender
 * @since
 */

public class MiaoDiSmsSender
{
    private final static Logger logger = LoggerFactory.getLogger(MiaoDiSmsSender.class);
    private static String ACCOUNT_SID;
    private static String AUTH_TOKEN;
    private static String URL;
    
    static{
        URL = Resources.SMS.getString("miaodi.url");
        ACCOUNT_SID= Resources.SMS.getString("miaodi.accountSid");
        AUTH_TOKEN = Resources.SMS.getString("miaodi.authToken");
    }
    
    @Async
    public static String smsSenderTo(String tel, String code, Map<String,Object> result)
    {
    	
        Date date = new Date();
        String timestamp = date.getTime()+"";
        String  sig= result.get("accountSid").toString()+result.get("authToken").toString()+timestamp;
        String sigMd5 =  DigestUtils.md5Hex(sig.getBytes());
        String params = "accountSid="+result.get("accountSid").toString()+"&templateid="+244660+"&to="+tel+"&timestamp="+timestamp+"&sig="+sigMd5+"&respDataType=JSON"+"&param="+code+",5";
        String res = HttpUtil.post(result.get("smsUrl").toString(), params);
        JSONObject json = JSONObject.parseObject(res);
        logger.info("json:"+json);
        if(json.getString("respCode").equals("0000"))
        {
          //发送成功
            return json.getString("smsId");
        }
        else{
            logger.info("短信发送失败，失败原因:"+json.getString("respDesc"));
        }
        return null;
    }
    

}
